{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据读取与检查"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "读取关系"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_rel = []\n",
    "sen = []\n",
    "for line in  open(\"../RE/data/RE_corpus.txt\",encoding= \"utf-8\") :\n",
    "    line = line.split()\n",
    "    if len(line) == 4:\n",
    "        sen.append(line[:3])\n",
    "    elif len(line) == 0:\n",
    "        data_rel.append(sen)\n",
    "        sen = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "35"
      ]
     },
     "execution_count": 154,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(data_rel)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "读取类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_type = []\n",
    "sen = []\n",
    "for line in  open(\"../RE/data/RE_corpus_detail.txt\",encoding= \"utf-8\") :\n",
    "    line = line.split()\n",
    "    if len(line) == 6:\n",
    "        temp = line[:]\n",
    "        temp[1] = line[1][1:-1]\n",
    "        temp[2] = line[2][:-1]\n",
    "\n",
    "        temp[4] = line[4][1:-1]\n",
    "        temp[5] = line[5][:-1]\n",
    "        sen.append(temp)\n",
    "    else:\n",
    "        data_type.append(sen)\n",
    "        sen = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "35"
      ]
     },
     "execution_count": 156,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(data_type)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "更新又方为又方id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "metadata": {},
   "outputs": [],
   "source": [
    "sentences = []\n",
    "temp = []\n",
    "for line in  open(\"../RE/data/RE_corpus.txt\",encoding= \"utf-8\") :\n",
    "    line = line.split()\n",
    "    if len(line) > 0:\n",
    "        temp.append(line[-1])\n",
    "    elif len(line) == 0:\n",
    "        sentences.append(temp[-1])\n",
    "        temp = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "35"
      ]
     },
     "execution_count": 158,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(sentences)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "21\n",
      "26\n",
      "34\n"
     ]
    }
   ],
   "source": [
    "for i in range(len(sentences)):\n",
    "    if sentences[i].find(\"又方\") != -1:\n",
    "        print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'NAM-202-204', 'NAM-157-159', 'NAM-192-194', 'NAM-99-101', 'NAM-70-72'}\n",
      "{'NAM-72-74'}\n",
      "{'NAM-76-78'}\n"
     ]
    }
   ],
   "source": [
    "name21 = set()\n",
    "\n",
    "for i,item in enumerate(data_type[21]):\n",
    "    if item[0] == \"NAM\" and data_rel[21][i][0] == \"又方\":\n",
    "        name21.add(\"NAM\" + \"-\" + str(item[1]) + \"-\" + str(item[2]))\n",
    "        \n",
    "    if item[3] == \"NAM\" and data_rel[21][i][1] == \"又方\":\n",
    "        name21.add(\"NAM\" + \"-\" + str(item[4]) + \"-\" + str(item[5])) \n",
    "        \n",
    "print(name21)\n",
    "##########################################################\n",
    "name26 = set()\n",
    "\n",
    "for i,item in enumerate(data_type[26]):\n",
    "    if item[0] == \"NAM\" and data_rel[26][i][0] == \"又方\":\n",
    "        name26.add(\"NAM\" + \"-\" + str(item[1]) + \"-\" + str(item[2]))\n",
    "        \n",
    "    if item[3] == \"NAM\" and data_rel[26][i][1] == \"又方\":\n",
    "        name26.add(\"NAM\" + \"-\" + str(item[4]) + \"-\" + str(item[5])) \n",
    "        \n",
    "print(name26)\n",
    "##########################################################\n",
    "name34 = set()\n",
    "\n",
    "for i,item in enumerate(data_type[34]):\n",
    "    if item[0] == \"NAM\" and data_rel[34][i][0] == \"又方\":\n",
    "        name34.add(\"NAM\" + \"-\" + str(item[1]) + \"-\" + str(item[2]))\n",
    "        \n",
    "    if item[3] == \"NAM\" and data_rel[34][i][1] == \"又方\":\n",
    "        name34.add(\"NAM\" + \"-\" + str(item[4]) + \"-\" + str(item[5])) \n",
    "        \n",
    "print(name34)\n",
    "##########################################################"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i,item in enumerate(data_type[21]):\n",
    "    if item[0] == \"NAM\":\n",
    "        if item[1] == \"70\" and item[2] == \"72\":\n",
    "            data_rel[21][i][0] = \"又方-id-1\"\n",
    "        elif item[1] == \"99\" and item[2] == \"101\":\n",
    "            data_rel[21][i][0] = \"又方-id-2\"\n",
    "        elif item[1] == \"157\" and item[2] == \"159\":\n",
    "            data_rel[21][i][0] = \"又方-id-3\"\n",
    "        elif item[1] == \"192\" and item[2] == \"194\":\n",
    "            data_rel[21][i][0] = \"又方-id-4\"\n",
    "        elif item[1] == \"202\" and item[2] == \"204\":\n",
    "            data_rel[21][i][0] = \"又方-id-5\"\n",
    "            \n",
    "    if item[3] == \"NAM\":\n",
    "        if item[4] == \"70\" and item[5] == \"72\":\n",
    "            data_rel[21][i][1] = \"又方-id-1\"\n",
    "        elif item[4] == \"99\" and item[5] == \"101\":\n",
    "            data_rel[21][i][1] = \"又方-id-2\"\n",
    "        elif item[4] == \"157\" and item[5] == \"159\":\n",
    "            data_rel[21][i][1] = \"又方-id-3\"\n",
    "        elif item[4] == \"192\" and item[5] == \"194\":\n",
    "            data_rel[21][i][1] = \"又方-id-4\"\n",
    "        elif item[4] == \"202\" and item[5] == \"204\":\n",
    "            data_rel[21][i][1] = \"又方-id-5\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i,item in enumerate(data_type[26]):\n",
    "    if item[0] == \"NAM\":\n",
    "        if item[1] == \"72\" and item[2] == \"74\":\n",
    "            data_rel[26][i][0] = \"又方-id-6\"\n",
    "        \n",
    "    if item[3] == \"NAM\":\n",
    "        if item[4] == \"72\" and item[5] == \"74\":\n",
    "            data_rel[26][i][1] = \"又方-id-6\"\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i,item in enumerate(data_type[34]):\n",
    "    if item[0] == \"NAM\":\n",
    "        if item[1] == \"76\" and item[2] == \"78\":\n",
    "            data_rel[34][i][0] = \"又方-id-7\"\n",
    "        \n",
    "    if item[3] == \"NAM\":\n",
    "        if item[4] == \"76\" and item[5] == \"78\":\n",
    "            data_rel[34][i][1] = \"又方-id-7\"\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['变蒸', '上四味为末', 'match']\n",
      "['SYM', '7', '9', 'USE', '78', '83']\n"
     ]
    }
   ],
   "source": [
    "print(data_rel[0][28])\n",
    "\n",
    "print(data_type[0][28])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "把顺序调换成【NAM USE】【NAM SYM】【USE SYM】【NAM DRU】【DRU QUA】"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['NAM', '0', '3', 'SYM', '7', '9']"
      ]
     },
     "execution_count": 165,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_type[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "metadata": {},
   "outputs": [],
   "source": [
    "def flag(item:list):\n",
    "    \n",
    "    flag = False\n",
    "    if item[0] == \"USE\" and item[3] == \"NAM\":\n",
    "        return True\n",
    "    if item[0] == \"SYM\" and item[3] == \"NAM\":\n",
    "        return True\n",
    "    if item[0] == \"SYM\" and item[3] == \"USE\":\n",
    "        return True\n",
    "    if item[0] == \"DRU\" and item[3] == \"NAM\":\n",
    "        return True\n",
    "    if item[0] == \"QUA\" and item[3] == \"DRU\":\n",
    "        return True\n",
    "    return flag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['变蒸', '上四味为末', 'match']\n",
      "['SYM', '7', '9', 'USE', '78', '83']\n"
     ]
    }
   ],
   "source": [
    "print(data_rel[0][28])\n",
    "\n",
    "print(data_type[0][28])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_type_copy = data_type[:]\n",
    "data_rel_copy = data_rel[:]\n",
    "\n",
    "for i,sen in enumerate(data_type_copy):\n",
    "    for j,item in enumerate(sen):\n",
    "        if flag(item):\n",
    "            temp_1 = item[3:]\n",
    "            temp_2 = item[:3]\n",
    "            data_type[i][j][:3] = temp_1\n",
    "            data_type[i][j][3:] = temp_2\n",
    "            \n",
    "            temp1 = data_rel_copy[i][j][0]\n",
    "            temp2 = data_rel_copy[i][j][1]\n",
    "            data_rel[i][j][0] = temp2\n",
    "            data_rel[i][j][1] = temp1\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['上四味为末', '变蒸', 'match']\n",
      "['USE', '78', '83', 'SYM', '7', '9']\n"
     ]
    }
   ],
   "source": [
    "print(data_rel[0][28])\n",
    "\n",
    "print(data_type[0][28])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "检查对齐"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "metadata": {},
   "outputs": [],
   "source": [
    "duiqi1 = []\n",
    "for i in data_rel:\n",
    "    duiqi1.append(len(i))\n",
    "    \n",
    "duiqi2 = []\n",
    "for i in data_type:\n",
    "    duiqi2.append(len(i))\n",
    "    \n",
    "for i in range(len(duiqi1)):\n",
    "    if duiqi1[i] != duiqi2[i]:\n",
    "        print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "写入三元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['上四味为末', '变蒸', 'match']\n",
      "['USE', '78', '83', 'SYM', '7', '9']\n"
     ]
    }
   ],
   "source": [
    "print(data_rel[0][28])\n",
    "\n",
    "print(data_type[0][28])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"data_res/triple.txt\",\"a\",encoding=\"utf-8\") as f:\n",
    "    for i,sen in enumerate(data_rel):\n",
    "        for j,item in enumerate(sen):\n",
    "            f.write(str(item[0]) + \" \" + str(item[1]) + \" \" + str(item[2]) + \" \" + \n",
    "                    str(data_type[i][j][0]) + \" \"  + str(data_type[i][j][1]) + \" \" +\n",
    "                    str(data_type[i][j][2]) + \" \" + str(data_type[i][j][3]) + \" \" +\n",
    "                    str(data_type[i][j][4]) + \" \" + str(data_type[i][j][5]) + \"\\n\")\n",
    "        f.write(\"\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "写入三元组（更细粒度）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_quantities(i,j,sen:list):\n",
    "    \"\"\"\n",
    "    [i,j]是药品\n",
    "    \"\"\"\n",
    "    for item in sen:\n",
    "        if item[2] == \"quantities\":\n",
    "            if item[4] == i and item[5] == j:\n",
    "                if item[3] == \"QUA\":\n",
    "                    return item[0]\n",
    "                elif item[6] == \"QUA\":\n",
    "                    return item[1]\n",
    "            elif item[7] == i and item[8] == j:\n",
    "                if item[3] == \"QUA\":\n",
    "                    return item[0]\n",
    "                elif item[6] == \"QUA\":\n",
    "                    return item[1]\n",
    "    return \"N/A\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_rel = []\n",
    "sen = []\n",
    "for line in  open(\"data_res/triple.txt\",encoding= \"utf-8\"):\n",
    "    line = line.split()\n",
    "    if len(line) >0:\n",
    "        sen.append(line)\n",
    "    elif len(line) == 0:\n",
    "        data_rel.append(sen)\n",
    "        sen = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 构建节点实体关系\n",
    "rels_name_dru = [] #　方名、草药之间的关系（关系的属性是药量）\n",
    "rels_name_anotherName = [] # 方名、又方之间的关系\n",
    "rels_name_use = [] # 方名、用法之间的关系\n",
    "rels_name_sym = [] # 方名、症状之间的关系\n",
    "rels_use_sym = [] # 用法、症状之间的关系\n",
    "\n",
    "\n",
    "# 文件中存有：【NAM USE uses】【NAM SYM cure】【USE SYM match】【NAM DRU contain】【DRU QUAquantities】\n",
    "for sen in data_rel:\n",
    "    for item in sen:\n",
    "        #  【NAM DRU contain】【quantities】\n",
    "        if item[2] == \"contain\":\n",
    "            if item[3] == \"NAM\" and item[6] == \"DRU\":\n",
    "                q = get_quantities(item[7],item[8],sen)\n",
    "                rels_name_dru.append([item[0],item[1],q])\n",
    "            else:\n",
    "                print(\"error:\",item)\n",
    "        #  【NAM USE uses】\n",
    "        elif item[2] == \"uses\":\n",
    "            if item[3] == \"NAM\" and item[6] == \"USE\":\n",
    "                rels_name_use.append([item[0],item[1]])\n",
    "            else:\n",
    "                print(\"error:\",item)\n",
    "        #  【NAM SYM cure】\n",
    "        elif item[2] == \"cure\":\n",
    "            if item[3] == \"NAM\" and item[6] == \"SYM\":\n",
    "                rels_name_sym.append([item[0],item[1]])\n",
    "            else:\n",
    "                print(\"error:\",item)\n",
    "        # 【USE SYM match】\n",
    "        elif item[2] == \"match\":\n",
    "            if item[3] == \"USE\" and item[6] == \"SYM\":\n",
    "                rels_name_sym.append([item[0],item[1]])\n",
    "            else: \n",
    "                print(\"error:\",item)\n",
    "        # 【NAM ANORTHERNAME unknown】\n",
    "        elif item[3] == \"NAM\" and item[6] == \"NAM\":\n",
    "            if item[0][:2] == \"又方\" or item[1][:2] == \"又方\":\n",
    "                rels_name_anotherName.append([item[0],item[1]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 实体库     \n",
    "NAM    \n",
    "SYM   \n",
    "USE    \n",
    "DRU   \n",
    "QUA   \n",
    "O    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "NAM = []\n",
    "SYM = []   \n",
    "USE = []    \n",
    "DRU = []   \n",
    "QUA = []\n",
    "\n",
    "def put_ls(i,j,string,flag):\n",
    "    \"\"\"\n",
    "    flag = 0\n",
    "    flag = 1\n",
    "    \"\"\"\n",
    "    if string == \"NAM\":\n",
    "        NAM.append(data_rel[i][j][flag])\n",
    "    elif string == \"SYM\":\n",
    "        SYM.append(data_rel[i][j][flag])\n",
    "    elif string == \"USE\":\n",
    "        USE.append(data_rel[i][j][flag])   \n",
    "    elif string == \"DRU\":\n",
    "        DRU.append(data_rel[i][j][flag])   \n",
    "    elif string == \"QUA\":\n",
    "        QUA.append(data_rel[i][j][flag]) \n",
    "    else:\n",
    "        return -1\n",
    "    return\n",
    "\n",
    "for i,sen in enumerate(data_type):\n",
    "    for j,item in enumerate(sen):\n",
    "        put_ls(i,j,item[0],0)\n",
    "        put_ls(i,j,item[3],1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(len(NAM))\n",
    "print(len(SYM))\n",
    "print(len(USE))\n",
    "print(len(DRU))\n",
    "print(len(QUA))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "NAM_set = set(NAM)\n",
    "with open(\"data_res/NAM_CORPUS.txt\",\"a\",encoding=\"utf-8\") as f:\n",
    "    for item in NAM_set:\n",
    "        f.write(item + \"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "SYM_set = set(SYM)\n",
    "with open(\"data_res/SYM_CORPUS.txt\",\"a\",encoding=\"utf-8\") as f:\n",
    "    for item in SYM_set:\n",
    "        f.write(item + \"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "USE_set = set(USE)\n",
    "with open(\"data_res/USE_CORPUS.txt\",\"a\",encoding=\"utf-8\") as f:\n",
    "    for item in USE_set:\n",
    "        f.write(item + \"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "DRU_set = set(DRU)\n",
    "with open(\"data_res/DRU_CORPUS.txt\",\"a\",encoding=\"utf-8\") as f:\n",
    "    for item in DRU_set:\n",
    "        f.write(item + \"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "QUA_set = set(QUA)\n",
    "with open(\"data_res/QUA_CORPUS.txt\",\"a\",encoding=\"utf-8\") as f:\n",
    "    for item in QUA_set:\n",
    "        f.write(item + \"\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "组成每个句子的三元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
